using HKB.Data.Dal;
using HKB.Data.Dal.S_RequestTableAdapters;
using System;
using System.Data;
namespace HKB.Web
{
	public class Shop_Request
	{
		private static readonly HLShop_RequestTableAdapter Adapter = new HLShop_RequestTableAdapter();
		public DataTable GetAllRequest()
		{
			return Shop_Request.Adapter.GetAllRequest();
		}
		public DataTable GetRequestByRequesterID(int UserID)
		{
			return Shop_Request.Adapter.GetRequestByRequesterID(UserID);
		}
		public DataTable GetRequestByID(int RequestID)
		{
			return Shop_Request.Adapter.GetRequestByID(RequestID);
		}
		public DataTable GetGetRequestListBySQL(string OrderBy, string ClassID, string Place, string UserID, string RequestType, string ProductName)
		{
			string text = "SELECT HLShop_Request.RequestID,RequesterID, HLShop_Request.RequestCategoryID,  HLShop_Request.RequestTitle, HLShop_Request.Description,   HLShop_Request.UploadImageUrl, HLShop_Request.RequestNumber,   HLShop_Request.ValidDate, HLShop_Request.Price, HLShop_Request.RequestPlace,   HLShop_Request.Remark, HLShop_Request.RequestClick,   HLShop_Request.RequestType, HLShop_Request.RequestKey,  HLShop_Request.CreateDate, HLShop_Request.EnableRequest, DATEDIFF(s,  GETDATE(), HLShop_Request.EndDate) AS LeftTime, HLShop_Request.EndDate,    HLShop_Request.Unit,   HL_User.UserName, HL_User.Email, HL_UserInfo.OICQ,    HL_UserInfo.Taobaowangwang, HL_UserInfo.NickName  FROM HL_User INNER JOIN  HLShop_Request ON HL_User.UserID = HLShop_Request.RequesterID    INNER JOIN  HL_UserInfo ON HLShop_Request.RequesterID = HL_UserInfo.UserID where 1=1 ";
			if (ProductName != "" && ProductName != null)
			{
				text = text + " and RequestTitle  like '%" + ProductName + "%' ";
			}
			if (UserID != "-1" && UserID != "")
			{
				text = text + " and RequesterID=" + UserID;
			}
			if (RequestType == "0" && RequestType != null)
			{
				text += " and DATEDIFF(s, GETDATE(), HLShop_Request.EndDate) >0";
			}
			if (RequestType == "1" && RequestType != null)
			{
				text += " and DATEDIFF(s, GETDATE(), HLShop_Request.EndDate) <0";
			}
			if (Place != "" && Place != null && Place != "-1")
			{
				text = text + " and RequestPlace like '%" + Place + "%' ";
			}
			if (ClassID != "-1" && ClassID != "" && ClassID != "0" && ClassID != null)
			{
				Shop_Class shop_Class = new Shop_Class();
				string chilenClass = shop_Class.GetChilenClass(int.Parse(ClassID));
				text = text + " AND RequestCategoryID IN (" + chilenClass + ")";
			}
			if (OrderBy != "" && OrderBy != null)
			{
				text = text + " ORDER BY " + OrderBy;
			}
			else
			{
				text += " ORDER BY LeftTime";
			}
			return Shop_Request.Adapter.GetGetRequestListBySQL(text);
		}
		public DataTable GetRequestByRequesterID(int UserID, string Type)
		{
			DataTable requestByRequesterID = Shop_Request.Adapter.GetRequestByRequesterID(UserID);
			DataView dataView = new DataView();
			dataView = requestByRequesterID.DefaultView;
			if (Type == "request_begin")
			{
				dataView.RowFilter = "LeftTime > 0";
			}
			else
			{
				if (Type == "request_end")
				{
					dataView.RowFilter = "LeftTime <= 0";
				}
			}
			return dataView.ToTable();
		}
		public void DeleteRequest(string RequestIDs)
		{
			string[] array = RequestIDs.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] != "")
				{
					Shop_Request.Adapter.DeleteRequest(int.Parse(array[i]));
				}
			}
		}
		public bool UpdateRequest(string ProductName, int ProductClassID, string ProductIntro, string ImageUrl, string Place, decimal Price, int ValidDay, string Remark, int ProductNumber, int RequestType, int EnableRequest, string pUnit, int RequestID)
		{
			S_Request.HLShop_RequestDataTable requestByID = Shop_Request.Adapter.GetRequestByID(RequestID);
			bool result;
			if (requestByID.Rows.Count <= 0)
			{
				result = false;
			}
			else
			{
				S_Request.HLShop_RequestRow hLShop_RequestRow = requestByID[0];
				hLShop_RequestRow.RequestTitle = ProductName;
				hLShop_RequestRow.RequestPlace = Place;
				hLShop_RequestRow.RequestNumber = ProductNumber;
				hLShop_RequestRow.Remark = Remark;
				hLShop_RequestRow.Price = Price;
				hLShop_RequestRow.Description = ProductIntro;
				hLShop_RequestRow.RequestCategoryID = ProductClassID;
				hLShop_RequestRow.EndDate = DateTime.Now.AddDays(Convert.ToDouble(ValidDay));
				hLShop_RequestRow.UploadImageUrl = ImageUrl;
				hLShop_RequestRow.RequestType = RequestType;
				hLShop_RequestRow.ValidDate = ValidDay;
				if (EnableRequest == 1)
				{
					hLShop_RequestRow.EnableRequest = true;
				}
				else
				{
					hLShop_RequestRow.EnableRequest = false;
				}
				hLShop_RequestRow.unit = pUnit;
				result = (Shop_Request.Adapter.Update(hLShop_RequestRow) >= 1);
			}
			return result;
		}
		public bool UpdateRequestClick(int RequestID)
		{
			S_Request.HLShop_RequestDataTable requestByID = Shop_Request.Adapter.GetRequestByID(RequestID);
			bool result;
			if (requestByID.Rows.Count <= 0)
			{
				result = false;
			}
			else
			{
				S_Request.HLShop_RequestRow hLShop_RequestRow = requestByID[0];
				hLShop_RequestRow.RequestClick++;
				result = (Shop_Request.Adapter.Update(hLShop_RequestRow) >= 1);
			}
			return result;
		}
		public bool AddRequest(int UserID, string ProductName, int ProductClassID, string ProductIntro, string ImageUrl, string Place, decimal Price, int ValidDay, string Remark, int ProductNumber, int RequestType, int EnableRequest, string pUnit)
		{
			S_Request.HLShop_RequestDataTable hLShop_RequestDataTable = new S_Request.HLShop_RequestDataTable();
			S_Request.HLShop_RequestRow hLShop_RequestRow = hLShop_RequestDataTable.NewHLShop_RequestRow();
			hLShop_RequestRow.RequestTitle = ProductName;
			hLShop_RequestRow.RequestPlace = Place;
			hLShop_RequestRow.RequesterID = UserID;
			hLShop_RequestRow.RequestNumber = ProductNumber;
			hLShop_RequestRow.RequestClick = 0;
			hLShop_RequestRow.Remark = Remark;
			hLShop_RequestRow.Price = Price;
			hLShop_RequestRow.Description = ProductIntro;
			hLShop_RequestRow.RequestCategoryID = ProductClassID;
			hLShop_RequestRow.EndDate = DateTime.Now.AddDays(Convert.ToDouble(ValidDay));
			hLShop_RequestRow.UploadImageUrl = ImageUrl;
			hLShop_RequestRow.RequestType = RequestType;
			hLShop_RequestRow.ValidDate = ValidDay;
			if (EnableRequest == 1)
			{
				hLShop_RequestRow.EnableRequest = true;
			}
			else
			{
				hLShop_RequestRow.EnableRequest = false;
			}
			hLShop_RequestRow.CreateDate = DateTime.Now;
			hLShop_RequestRow.unit = pUnit;
			hLShop_RequestDataTable.AddHLShop_RequestRow(hLShop_RequestRow);
			int num = Shop_Request.Adapter.Update(hLShop_RequestDataTable);
			return num == 1;
		}
	}
}
